﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MNS.Activities.Common;

namespace MNS.Activities.Editors.TimeEditor
{
    public partial class TimeEditorDialog : Form
    {
        public TimeEditorDialog()
        {
            InitializeComponent();
        }

        protected Time _PropertyValue = Time.Default;
        public Time PropertyValue
        {
            get { return _PropertyValue; }
            set
            {
                _PropertyValue = value;
                if (value == null)
                    _PropertyValue = Time.Default;
                SetUI(_PropertyValue);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            PropertyValue = GetFromUI();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            PropertyValue = Time.Default;
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void chbxIsSet_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chbx = sender as CheckBox;
            SetEnable(chbx.Checked);
        }

        protected void SetEnable(bool isSet)
        {
            if (isSet)
            {
                udHours.Enabled = true;
                udMinutes.Enabled = true;
                udSeconds.Enabled = true;
                udMilliSeconds.Enabled = true;
            }
            else
            {
                udHours.Value = 0;
                udHours.Enabled = false;
                udMinutes.Value = 0;
                udMinutes.Enabled = false;
                udSeconds.Value = 0;
                udSeconds.Enabled = false;
                udMilliSeconds.Value = 0;
                udMilliSeconds.Enabled = false;
            }
        }

        protected void SetUI(Time value)
        {
            if (value == null)
                value = Time.Default;

            chbxIsSet.Checked = value.IsSet;
            SetEnable(value.IsSet);
            if (value.IsSet)
            {
                udHours.Value = value.Hours;
                udMinutes.Value = value.Minutes;
                udSeconds.Value = value.Seconds;
                udMilliSeconds.Value = value.MilliSeconds;
            }
        }

        protected Time GetFromUI()
        {
            Time value = Time.Default;

            if (!chbxIsSet.Checked)
                return value;
            else
            {
                value = new Time()
                    {
                        IsSet = true,
                        Hours = (int)udHours.Value,
                        Minutes = (int)udMinutes.Value,
                        Seconds = (int)udSeconds.Value,
                        MilliSeconds = (int)udMilliSeconds.Value,
                    };
            }

            return value;
        }
    }
}
